home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / old-stream / istream.cc < prev    next >
C/C++ Source or Header  |  1992-01-17  |  10KB  |  504 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1989 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. /* *** Version 1.2 -- nearly 100% AT&T 1.2 compatible *** */
  20.  
  21. #ifdef __GNUG__
  22. #pragma implementation
  23. #endif
  24. #include <stream.h>
  25. #include <stdarg.h>
  26. #include <values.h>
  27. #include <ctype.h>
  28. #include <Obstack.h>
  29.  
  30.  
  31. istream::istream(streambuf* s, int sk, ostream* t)
  32.      : bp(s), state(_good), skipws(sk), tied_to(t), ownbuf(0) {}
  33.  
  34. istream::istream(int sz, char* buf, int sk, ostream* t)
  35.      : state(_good), skipws(sk), tied_to(t), ownbuf(1)
  36. {
  37.   bp = new streambuf;
  38.   if (buf == 0)
  39.   {
  40.     bp->alloc = 1;
  41.     buf = new char[sz];
  42.   }
  43.   else
  44.     bp->alloc = 0;
  45.  
  46.   bp->setbuf(buf, sz, sz);
  47. }
  48.  
  49. istream::~istream()
  50. {
  51.   if (ownbuf) delete bp;
  52. }
  53.  
  54. istream::istream(const char* filename, io_mode m, access_mode a, int sk, ostream* t)
  55.      : state(_good), skipws(sk), tied_to(t), ownbuf(1)
  56. {
  57.   bp = new Filebuf(filename, m, a);
  58. }
  59.  
  60. istream::istream(const char* filename, const char* m, int sk, ostream* t)
  61.      : state(_good), skipws(sk), tied_to(t), ownbuf(1)
  62. {
  63.   bp = new Filebuf(filename, m);
  64. }
  65.  
  66. istream::istream(int filedesc, io_mode m, int sk, ostream* t)
  67.      : state(_good), skipws(sk), tied_to(t), ownbuf(1)
  68. {
  69.   bp = new Filebuf(filedesc, m);
  70. }
  71.  
  72. istream::istream(FILE* fileptr, int sk, ostream* t)
  73.      : state(_good), skipws(sk), tied_to(t), ownbuf(1)
  74. {
  75.   bp = new Filebuf(fileptr);
  76.                   
  77. }
  78.  
  79. istream::istream(int filedesc, int sk, ostream* t)
  80.      : state(_good), skipws(sk), tied_to(t), ownbuf(1)
  81. {
  82.   bp = new filebuf(filedesc);
  83. }
  84.  
  85. istream::istream(int filedesc, char* buf, int buflen, int sk, ostream* t)
  86.      : state(_good), skipws(sk), tied_to(t), ownbuf(1)
  87. {
  88.   bp = new filebuf(filedesc, buf, buflen);
  89. }
  90.  
  91. istream&  istream::open(const char* filename, io_mode m, access_mode a)
  92. {
  93.   return failif(bp->open(filename, m, a) == 0);
  94. }
  95.  
  96. istream&  istream::open(const char* filename, const char* m)
  97. {
  98.   return failif(bp->open(filename, m) == 0);
  99. }
  100.  
  101. istream&  istream::open(int  filedesc, io_mode m)
  102. {
  103.   return failif(bp->open(filedesc, m) == 0);
  104. }
  105.  
  106. istream&  istream::open(FILE* fileptr)
  107. {
  108.   return failif(bp->open(fileptr) == 0);
  109. }
  110.  
  111. istream&  istream::open(const char* filenam, open_mode m)
  112. {
  113.   return failif(bp->open(filenam, m) == 0);
  114. }
  115.  
  116. istream& istream::get(char& c)
  117. {
  118.   if (good())
  119.   {
  120.     if(tied_to != 0) tied_to->flush();
  121.     int ch = bp->sgetc();
  122.     if (ch == EOF) 
  123.       set(_eof);
  124.     else
  125.     {
  126.       c = ch;
  127.       bp->stossc();
  128.     }
  129.   }
  130.   return *this;
  131. }
  132.  
  133.  
  134. istream& istream::operator >> (whitespace&)
  135. {
  136.   if (good())
  137.   {
  138.     int ch;
  139.     if(tied_to != 0) tied_to->flush();
  140.     while (((ch = bp->sgetc()) != EOF) && isspace(ch)) bp->stossc();
  141.     if (ch == EOF) set(_eof);
  142.   }
  143.   return *this;
  144. }
  145.  
  146.  
  147. istream& istream::operator >> (char& c)
  148. {
  149.   if (skipws) (*this >> WS);
  150.   return get(c);
  151. }
  152.  
  153. istream& istream::get(char* s, int n, char terminator)
  154. {
  155.   if (!readable())
  156.   {
  157.     set(_fail);
  158.     return *this;
  159.   }
  160.  
  161.   char ch = 0;
  162.   char* start = s;
  163.   if (--n > 0 && get(ch))
  164.   {
  165.     if (ch == terminator) 
  166.       unget(ch);
  167.     else
  168.     {
  169.       *s++ = ch; --n;
  170.       while (n-- > 0 && get(ch))
  171.       {
  172.         if (ch == terminator)
  173.         {
  174.           unget(ch);
  175.           break;
  176.         }
  177.         else
  178.           *s++ = ch;
  179.       }
  180.     }
  181.   }
  182.  
  183.   *s = 0;
  184.   if (s != start) clear();
  185.   return *this;
  186. }
  187.  
  188.  
  189. istream& istream::operator >> (char* s)
  190. {
  191.   if (!readable() || s == 0)
  192.   {
  193.     set(_fail);
  194.     return *this;
  195.   }
  196.  
  197.   if (skipws && !(*this >> WS)) return *this;
  198.  
  199.   char ch;
  200.   char* start = s;
  201.   if (get(ch))
  202.   {
  203.     *s++ = ch;
  204.     while (get(ch))
  205.     {
  206.       if (isspace(ch))
  207.       {
  208.         unget(ch);
  209.         break;
  210.       }
  211.       else
  212.         *s++ = ch;
  213.     }
  214.   }
  215.  
  216.   *s = 0;
  217.   if (s != start) clear();
  218.   return *this;
  219. }
  220.  
  221.  
  222. istream& istream::getline(char* s, int n, char terminator)
  223. {
  224.   if (!readable())
  225.   {
  226.     set(_fail);
  227.     return *this;
  228.   }
  229.   char* start = s;
  230.   char ch;
  231.   while (--n > 0 && get(ch) && ((*s++ = ch) != terminator));
  232.  
  233.   *s = 0;
  234.   if (s != start) clear();
  235.   return *this;
  236. }
  237.  
  238. // from Doug Schmidt
  239.  
  240. // This should probably be a page size....
  241. #define CHUNK_SIZE 512
  242.  
  243. /* Reads an arbitrarily long input line terminated by a user-specified
  244.    TERMINATOR.  Super-nifty trick using recursion avoids unnecessary calls
  245.    to NEW! */
  246.  
  247. char *istream::readline (int chunk_number, char terminator) 
  248. {
  249.   char buf[CHUNK_SIZE];
  250.   register char *bufptr = buf;
  251.   register char *ptr;
  252.   char ch;
  253.   int continu;
  254.  
  255.   while ((continu = !!get(ch)) && ch != terminator) /* fill the current buffer */
  256.     {
  257.       *bufptr++ = ch;
  258.       if (bufptr - buf >= CHUNK_SIZE) /* prepend remainder to ptr buffer */
  259.         {
  260.           if (ptr = readline (chunk_number + 1, terminator))
  261.  
  262.             for (; bufptr != buf; *--ptr = *--bufptr);
  263.  
  264.           return ptr;
  265.         }
  266.     }
  267.   if (!continu && bufptr == buf)
  268.     return NULL;
  269.  
  270.   int size = (chunk_number * CHUNK_SIZE + bufptr - buf) + 1;
  271.  
  272.   if (ptr = new char[size])
  273.     {
  274.  
  275.       for (*(ptr += (size - 1)) = '\0'; bufptr != buf; *--ptr = *--bufptr)
  276.         ;
  277.  
  278.       return ptr;
  279.     } 
  280.   else 
  281.     return NULL;
  282. }
  283.  
  284. /* Reads an arbitrarily long input line terminated by TERMINATOR.
  285.    This routine allocates its own memory, so the user should
  286.    only supply the address of a (char *). */
  287.  
  288. istream& istream::gets(char **s, char terminator)
  289. {
  290.   return failif(!readable() || !(*s = readline (0, terminator)));
  291. }
  292.   
  293. istream& istream::operator >> (long& y)
  294. {
  295.   if (!readable())
  296.   {
  297.     set(_bad);
  298.     return *this;
  299.   }
  300.  
  301.   int got_one = 0;
  302.   char sgn = 0;
  303.   char ch;
  304.   y = 0;
  305.   if (skipws) *this >> WS;
  306.   if (!good()) 
  307.   {
  308.     set(_bad);
  309.     return *this;
  310.   }
  311.   while (get(ch))
  312.   {
  313.     if (ch == '-')
  314.     {
  315.       if (sgn == 0 && got_one == 0)
  316.         sgn = '-';
  317.       else
  318.         break;
  319.     }
  320.     else if (ch >= '0' && ch <= '9')
  321.       y = y * 10 + ((got_one = ch) - '0');
  322.     else
  323.       break;
  324.   }
  325.   if (good())
  326.     unget(ch);
  327.   if (!got_one)
  328.     set(_bad);
  329.   else
  330.     clear();
  331.  
  332.   if (sgn == '-')
  333.     y = -y;
  334.  
  335.   return *this;
  336. }
  337.  
  338. istream& istream::operator >> (unsigned long& y)
  339. {
  340.   if (!readable())
  341.   {
  342.     set(_bad);
  343.     return *this;
  344.   }
  345.  
  346.   int got_one = 0;
  347.   char ch;
  348.   y = 0;
  349.   if (skipws) *this >> WS;
  350.   if (!good())
  351.   while (get(ch))
  352.   {
  353.     if (ch >= '0' && ch <= '9')
  354.       y = y * 10 + ((got_one = ch) - '0');
  355.     else
  356.       break;
  357.   }
  358.   if (good())
  359.     unget(ch);
  360.   if (!got_one)
  361.     set(_bad);
  362.   else
  363.     clear();
  364.   return *this;
  365. }
  366.  
  367.  
  368. /* for input to a double, we must trust atof (cannot even use
  369. the better strtod since it is not universally supported). So
  370. guaranteed legal chars are gathered up into an obstack. The
  371. only possible, undiagnosable error is that the input number
  372. might give a floating overflow or underflow inside atof. 
  373. I know of no way to avoid this */
  374.  
  375. extern Obstack _libgxx_io_ob;
  376.  
  377. istream& istream::operator >> (double & y)
  378. {
  379.   if (!readable())
  380.   {
  381.     set(_bad);
  382.     return *this;
  383.   }
  384.  
  385.  
  386.   char seenint = 0;
  387.   char seendec = 0;
  388.   char seenexp = 0;
  389.   char seensgn = 0;
  390.   char seene = 0;
  391.   char seenexpsgn = 0;
  392.   char seendot = 0;
  393.   char ch;
  394.  
  395.   if (skipws) *this >> WS;
  396.   if (!good()) 
  397.   {
  398.     set(_bad);
  399.     return *this;
  400.   }
  401.   while (get(ch))
  402.   {
  403.     if (ch == '-' || ch == '+')
  404.     {
  405.       if (seene && !seenexpsgn)
  406.         _libgxx_io_ob.grow(seenexpsgn = ch);
  407.       else if (!seensgn && !seenint)
  408.         _libgxx_io_ob.grow(seensgn = ch);
  409.       else
  410.         break;
  411.     }
  412.     else if (ch == '.' && !seendot)
  413.     {
  414.       _libgxx_io_ob.grow(seendot = ch);
  415.     }
  416.     else if ((ch == 'e' || ch == 'E') && !seene)
  417.     {
  418.       _libgxx_io_ob.grow(seene = ch);
  419.     }
  420.     else if (ch >= '0' && ch <= '9')
  421.     {
  422.       _libgxx_io_ob.grow(ch);
  423.       if (seene) seenexp = ch;
  424.       else if (seendot) seendec = ch;
  425.       else seenint = ch;
  426.     }
  427.     else
  428.       break;
  429.   }
  430.   char* str = (char *) _libgxx_io_ob.finish(0);
  431.   if (good())
  432.     unget(ch);
  433.   if ((seenint || seendec) && (!seene || seenexp))
  434.     y = atof(str);
  435.   else
  436.     set(_bad);
  437.   _libgxx_io_ob.free(str);
  438.   return *this;
  439. }
  440.  
  441. istream& istream::operator >> (int& y)
  442. {
  443.   long l; (*this >> l); y = int(l); return *this;
  444. }
  445.  
  446. istream& istream:: operator >> (unsigned int& y)
  447. {
  448.   long l; (*this >> l); y = (unsigned int)(l); return *this;
  449. }
  450.  
  451. istream& istream:: operator >> (short& y)
  452. {
  453.   long l; (*this >> l); y = short(l); return *this;
  454. }
  455.  
  456. istream& istream:: operator >> (unsigned short& y)
  457. {
  458.   long l; (*this >> l); y = (unsigned short)(l); return *this;
  459. }
  460.  
  461. istream& istream:: operator >> (float& y)
  462. {
  463.   double d; (*this >> d); y = float(d); return *this;
  464. }
  465.  
  466. const char* istream::name()
  467. {
  468.   return bp->name();
  469. }
  470.  
  471. void istream::error()
  472. {
  473.   bp->error();
  474. }
  475.  
  476. ostream* istream::tie(ostream* s)
  477. {
  478.   ostream* was = tied_to; tied_to = s; return was;
  479. }
  480.  
  481. void istream::_flush()
  482. {
  483.   if(tied_to != 0) tied_to->flush();
  484. }
  485.  
  486.  
  487. //--------------------------------------------------------------
  488.  
  489. extern ostream cout;
  490.  
  491. #ifndef DEFAULT_filebuf
  492.  
  493. istream  cin(stdin, 1, &cout);
  494.  
  495. #else
  496.  
  497. static char cinbuf[BUFSIZE];
  498. istream cin (0, cinbuf,  BUFSIZE, 1, &cout);
  499.  
  500. #endif
  501.  
  502. whitespace WS;
  503. whitespace ws;
  504.